﻿using StackExchange.Redis;

namespace Service.Core.Redis.Services.Impl;

public class RedisServiceImpl : IRedisService
{
   

    /// <summary>
    /// get database
    /// </summary>
    public IDatabase Database { get; }
    public IConnectionMultiplexer Multiplexer { get; }
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="database"></param>
    /// <param name="multiplexer"></param>
    public RedisServiceImpl(IConnectionMultiplexer multiplexer)
    {
        Multiplexer = multiplexer;
        Database = multiplexer.GetDatabase();
    }
    

    public void Dispose()
    {
        if (Multiplexer is null) return;
        Multiplexer.Close();
        Multiplexer.Dispose();
    }

    /// <summary>
    /// 获取指定Redis数据库
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public IDatabase UseDatabase(int db)
    {
        return Multiplexer.GetDatabase(db);
    }
}
